#ifndef __DOUBLELINKLIST_H_
#define __DOUBLELINKLIST_H_

#include <stddef.h>
#include "DynamicArray.h"

class DoubleLinkList
{
public:
    using ElementType = void*;//void*
    DoubleLinkList();
    // 拷贝构造  赋值运算符重载
    DoubleLinkList(const DoubleLinkList& list);
    DoubleLinkList& operator=(const DoubleLinkList& list);

    ~DoubleLinkList();

    void SetFreeFunc(void (*Free)(ElementType ));
    void SetCompareFunc(int (*Compare)(ElementType,ElementType));
    void SetPrintFunc(void (*Print)(ElementType));

    void InsertTail(const ElementType& element);
    void InsertByIndex(size_t index, const ElementType& element);

    //删除 2个
    //按位置删除
    void RemoveByIndex(size_t index);
    //按值删除
    void RemoveByElement(const ElementType& element);

    //查找 2个 
    // 按位置查找
    ElementType *FindByIndex(size_t index);
    // 按值查找
    DARRAY::DynamicArray FindByElement(const ElementType &element);

    //修改 2个
    //按位置查找
    void UpdateByIndex(size_t index, const ElementType &element);
    //按值查找
    void UpdateByElement(const ElementType &oldValue, const ElementType &newValue);

    //排序 快速排序
    void Sort();

    void show();
private:
    struct Node
    {
        Node(const ElementType& value)
        {
            this->value = value;
            next = prev = nullptr;
        }
        ElementType value;
        Node *prev;  //前驱指针
        Node *next;  //后驱指针
    };


    Node *head;  //头指针
    Node *tail;  //尾指针
    size_t size;
    void FastSort(Node *start, Node *end);
    void (*Free)(ElementType);  //释放
    int (*Compare)(ElementType, ElementType);  //比较
    void (*Print)(ElementType);  //打印
};


#endif